<?php if(!isset($_SESSION)){
  session_start(); $message="";
}
require_once('../../Connections/microassur.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  $updateSQL = sprintf("UPDATE justificatif SET Type_justificatif=%s, Valeur_justificatif=%s, Identifiant_contrat=%s, Identifiant_personne=%s, Identifiant_evenement=%s, Identifiant_sinistre=%s WHERE Identifiant_justificatif=%s",
                       GetSQLValueString($_POST['Type_justificatif'], "text"),
                       GetSQLValueString($_POST['Valeur_justificatif'], "text"),
                       GetSQLValueString($_SESSION['idcontrat'], "int"),
                       GetSQLValueString($_POST['Identifiant_personne'], "int"),
                       GetSQLValueString($_POST['Identifiant_evenement'], "int"),
                       GetSQLValueString($_POST['Identifiant_sinistre'], "int"),
                       GetSQLValueString($_POST['Identifiant_justificatif'], "int"));

  mysql_select_db($database_microassur, $microassur);
  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
  if(!$Result1){  
	  header("location:index.php?mod=echec&Identifiant_personne=".$_GET['Identifiant_personne']);
   }
   else 	  header("location:index.php?mod=success&Identifiant_personne=".$_GET['Identifiant_personne']);

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO justificatif (Identifiant_justificatif, Type_justificatif, Valeur_justificatif, Identifiant_contrat, Identifiant_personne, Identifiant_evenement, Identifiant_sinistre) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Identifiant_justificatif'], "int"),
                       GetSQLValueString($_POST['Type_justificatif'], "text"),
                       GetSQLValueString($_POST['Valeur_justificatif'], "text"),
                       GetSQLValueString($_SESSION['idcontrat'], "int"),
                       GetSQLValueString($_SESSION['idpers'], "int"),
                       GetSQLValueString($_POST['Identifiant_evenement'], "int"),
                       GetSQLValueString($_POST['Identifiant_sinistre'], "int"));

  mysql_select_db($database_microassur, $microassur);
  $Result1 = mysql_query($insertSQL, $microassur) or die(mysql_error());
  if(!$Result1){  
	  header("location:index.php?ajout=echec&Identifiant_personne=".$_GET['Identifiant_personne']);
   }
   else  header("location:index.php?ajout=success&Identifiant_personne=".$_GET['Identifiant_personne']);
}

$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_justificatif'])) {
  $colname_rs_mod = $_GET['Identifiant_justificatif'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod = sprintf("SELECT * FROM justificatif WHERE Identifiant_justificatif = %s", GetSQLValueString($colname_rs_mod, "int"));
$rs_mod = mysql_query($query_rs_mod, $microassur) or die(mysql_error());
$row_rs_mod = mysql_fetch_assoc($rs_mod);
$totalRows_rs_mod = mysql_num_rows($rs_mod);

$colname_rs1 = "-1";
if (isset($_GET['Identifiant_justificatif'])) {
  $colname_rs1 = $_GET['Identifiant_justificatif'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs1 = sprintf("SELECT * FROM justificatif WHERE Identifiant_justificatif = %s", GetSQLValueString($colname_rs1, "int"));
$rs1 = mysql_query($query_rs1, $microassur) or die(mysql_error());
$row_rs1 = mysql_fetch_assoc($rs1);
$totalRows_rs1 = mysql_num_rows($rs1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
</head>

<body>
<?php if(!isset($_GET['Identifiant_justificatif'])) {?>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">TYPE:</td>
      <td><select name="Type_justificatif">
        <option value="CID">Carte d’identité</option>
        <option value="PA">Passeport</option>
        <option value="CD">Certificat décès</option>
        <option value="CIN">Certificat invalidité</option>
      </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">VALEUR</td>
      <td><input type="text" name="Valeur_justificatif" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Valider" /> 
      <a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']?>">Annuler</a></td>
    </tr>
  </table>
  <input type="hidden" name="Identifiant_justificatif" value="" />
  <input type="hidden" name="Identifiant_contrat" value="" />
  <input type="hidden" name="Identifiant_personne" value="" />
  <input type="hidden" name="Identifiant_evenement" value="" />
  <input type="hidden" name="Identifiant_sinistre" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<?php }
else
{?>
<p>&nbsp;</p>


<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">TYPE:</td>
      <td><select name="Type_justificatif">
        <option value="CID" <?php if (!(strcmp("CID", htmlentities($row_rs_mod['Type_justificatif'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>>Carte d'identité</option>
        <option value="PA" <?php if (!(strcmp("PA", htmlentities($row_rs_mod['Type_justificatif'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>>Passeport</option>
        <option value="CD" <?php if (!(strcmp("CD", htmlentities($row_rs_mod['Type_justificatif'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>>Certificat décès</option>
        <option value="CIN" <?php if (!(strcmp("CIN", htmlentities($row_rs_mod['Type_justificatif'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>>Certificat invalidité</option>
      </select>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">VALEUR:</td>
      <td><input type="text" name="Valeur_justificatif" value="<?php echo htmlentities($row_rs_mod['Valeur_justificatif'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Valider" />
      <a href="index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']?>">Annuler</a></td>
    </tr>
  </table>
  <input type="hidden" name="Identifiant_justificatif" value="<?php echo $row_rs_mod['Identifiant_justificatif']; ?>" />
  <input type="hidden" name="Identifiant_contrat" value="<?php echo htmlentities($row_rs_mod['Identifiant_contrat'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="Identifiant_personne" value="<?php echo htmlentities($row_rs_mod['Identifiant_personne'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="Identifiant_evenement" value="<?php echo htmlentities($row_rs_mod['Identifiant_evenement'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="Identifiant_sinistre" value="<?php echo htmlentities($row_rs_mod['Identifiant_sinistre'], ENT_COMPAT, 'utf-8'); ?>" />
  <input type="hidden" name="MM_update" value="form2" />
  <input type="hidden" name="Identifiant_justificatif" value="<?php echo $row_rs_mod['Identifiant_justificatif']; ?>" />
</form>
<p>&nbsp;</p>
<?php }?>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($rs1);

mysql_free_result($rs_mod);

?>
